package se.topshelf.golftracker.web.controller.course;

import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.Validator;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import se.topshelf.golftracker.core.domain.Course;
import se.topshelf.golftracker.core.domain.MemberClub;
import se.topshelf.golftracker.core.service.BaseService;
import se.topshelf.golftracker.core.service.CourseService;
import se.topshelf.golftracker.web.controller.BaseEntityController;
import se.topshelf.golftracker.web.editor.BaseEntityEditor;

import javax.annotation.Resource;

/**
 *
 */
@Controller
@SessionAttributes("entity")
@RequestMapping("/course")
public class CourseController extends BaseEntityController<Course> {
    static final String ALL_MEMBER_CLUBS_MODEL_ATTRIBUTE = "allMemberClubs";
    public static final String COURSE_CONTEXT_PATH = "course";
    @Resource  // Automatically set ("wired") by Spring
    private CourseService courseService;

    public CourseController() {
        super(Course.class);
    }

    @InitBinder
    protected void initBinder(WebDataBinder binder) {
        binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
        binder.registerCustomEditor(MemberClub.class, "memberClub", new BaseEntityEditor<MemberClub>(MemberClub.class, courseService));
    }

    @Override
    protected Validator getValidator() {
        return new CourseValidator(courseService);
    }

    @Override
    protected String getEntityUrlContextPath() {
        return COURSE_CONTEXT_PATH;
    }

    @Override
    protected BaseService getBaseService() {
        return courseService;
    }

    @Override
    protected void addModelReferenceData(ModelMap model, Course entity) {
        model.put(ALL_MEMBER_CLUBS_MODEL_ATTRIBUTE, super.fetchAllEntitiesAndBuildReferenceDataFor(MemberClub.class));

    }
}
